﻿
Imports storoomc.storoomWM

Namespace storoomWM
    Public Module storoomWM
        Public Delegate Function convertDelegate(Of T)(ByVal input As T, ByRef output As bytes) As Boolean
        Friend Delegate Function wmDelegate(ByVal key As String, ByVal buff As bytes) As Boolean

        Friend Function wm(Of T)(ByVal key As String, ByVal input As T, _
                                 ByVal convert As convertDelegate(Of T), _
                                 ByVal wmoper As wmDelegate) As Boolean
            Dim buff As bytes = Nothing
            If Not convert(input, buff) Then
                raiseError("cannot convert input from " + GetType(T).ToString() + " to bytes" _
                           , errorHandle.errorType.warning)
                Return False
            Else
                If Not wmoper(key, buff) Then
                    raiseError("cannot write/modify to storoom, key " + key, errorHandle.errorType.warning)
                    Return False
                Else
                    Return True
                End If
            End If
        End Function
    End Module
End Namespace

Namespace storoomWriter
    Public Module storoomWriter
        Public Function write(Of T)(ByVal key As String, ByVal input As T, _
                                    ByVal convert As storoomWM.convertDelegate(Of T)) As Boolean
            Return wm(Of T)(key, input, convert, AddressOf storoom.write)
        End Function

        Public Function write(ByVal key As String, ByVal input As Byte) As Boolean
            Return write(Of Byte)(key, input, AddressOf convertor.tobytes)
        End Function

        Public Function write(ByVal key As String, ByRef output As Int16) As Boolean
            Return write(Of Int16)(key, output, AddressOf convertor.tobytes)
        End Function

        Public Function write(ByVal key As String, ByRef output As Int32) As Boolean
            Return write(Of Int32)(key, output, AddressOf convertor.tobytes)
        End Function

        Public Function write(ByVal key As String, ByRef output As Int64) As Boolean
            Return write(Of Int64)(key, output, AddressOf convertor.tobytes)
        End Function

        Public Function write(ByVal key As String, ByRef output As String) As Boolean
            Return write(Of Int64)(key, output, AddressOf convertor.toutf8bytes)
        End Function
    End Module
End Namespace

Namespace storoomModifier
    Public Module storoomModifier
        Public Function modify(Of T)(ByVal key As String, ByVal input As T, _
                                     ByVal convert As storoomWM.convertDelegate(Of T)) As Boolean
            Return wm(Of T)(key, input, convert, AddressOf storoom.modify)
        End Function

        Public Function modify(ByVal key As String, ByVal input As Byte) As Boolean
            Return modify(Of Byte)(key, input, AddressOf convertor.tobytes)
        End Function

        Public Function modify(ByVal key As String, ByRef output As Int16) As Boolean
            Return modify(Of Int16)(key, output, AddressOf convertor.tobytes)
        End Function

        Public Function modify(ByVal key As String, ByRef output As Int32) As Boolean
            Return modify(Of Int32)(key, output, AddressOf convertor.tobytes)
        End Function

        Public Function modify(ByVal key As String, ByRef output As Int64) As Boolean
            Return modify(Of Int64)(key, output, AddressOf convertor.tobytes)
        End Function

        Public Function modify(ByVal key As String, ByRef output As String) As Boolean
            Return modify(Of Int64)(key, output, AddressOf convertor.toutf8bytes)
        End Function
    End Module
End Namespace
